<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
if($_RQ['op']=='get'){
	$setting=uni_setting('pay');
	if(!$setting['credit']){
		exi('系统暂不支持充值！','error');
	}
	$return['credit_activitys']=$setting['credit_activitys']?:array();
	$return['item']=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('credit2','credit3'));
	$newcoupons=model('jy_weishop/coupon')->getmycoupon_ing(2,$goodsprice);
	$return['item']['couponid']=$newcoupons[0]['id'];
	$return['coupons']=$newcoupons;
	$return['post']=array('type'=>2);
	$return['pluginset']['jifen_rate']=$return['pluginset']['jifen_rate']?:1;
	exi($return);
}
if($_RQ['op']=='recharge'){
	$post=get_postdata();
	if($post['type']==3){
		if(intval($post['jifen'])<=0){
			exi('请先设置额度！并且为整数！','error');
		}
		$this->setting['jifen_rate']=$this->setting['jifen_rate']?:1;
		$jifen_rate=round($this->setting['jifen_rate'],2)*1;
		$fee=$post['jifen']/$jifen_rate;
	}else{
		$fee=intval($post['money']*100)/100;
		if(!$fee || $fee<=0){
			exi('请先设置充值金额！并且为数字！','error');
		}
	}
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'fee'=>$fee,
		'type'=>$post['type'],
		'createtime'=>TIMESTAMP
	);
	if($post['type']==3){
		$data['credit']=$post['jifen'];
	}else{
		$data['couponid']=intval($post['couponid']);
	}
	pdo_insert('core_recharge',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'recharge',
		'id'=>$id,
		'fee'=>$fee,
		'title'=>$post['type']==3?'金币充值':'余额充值',
		'plugin'=>'jy_ailiao',
		//'pay_type'=>'wechat',
		'no_type'=>array('hdfk','credit'),
		'back_url'=>'jy_ailiao/member.recharge/pay_result/',
		'success_url'=>'jy_ailiao/member.index/',
		'error_url'=>'jy_ailiao/member.index/',
	);
	if($post['type']==3){
		$params['no_type']=array('hdfk');
	}
	if($_SESSION['container']=='aliapp' && $_SESSION['aliappopenid']){
		$params['pay_type']='alipay';
	}
	$pay_id=cfc('pay')->create_pay_order($params);
	pdo_update('core_recharge',array('pay_id'=>$pay_id),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi($pay_id);
}
if($_RQ['op']=='pay_result'){
	$payresult=$_SESSION['pay_result'];
	$recharge=pdo_get('core_recharge',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($recharge['fee']!=$payresult['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($recharge['status']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($payresult['status']!=1){
		exi('充值失败！','error');
	}
	pdo_update('core_recharge',array('status'=>1,'pay_type'=>$payresult['pay_type']),array('id'=>$recharge['id'],'uniacid'=>$_SESSION['uniacid']));
	if($recharge['type']==3){
		cfc('core')->member_credit($_SESSION['uid'],'credit3',$recharge['credit'],1,'jy_ailiao','会员自助充值');
		$recharge['ordersn']=$recharge['id'];
		$recharge['money']=$recharge['fee'];
		$this->model('fuwu')->create($recharge);
	}else{
		cfc('core')->member_credit($_SESSION['uid'],'credit2',$recharge['fee'],1,'jy_ailiao','会员自助充值');
		$setting=uni_setting('pay');
		if($setting['credit_activitys']){
			//充值赠送
			$give=0;
			foreach($setting['credit_activitys'] as $item){
				if($recharge['fee']>=$item['condition'] && $item['money']>$give){
					$give=$item['money'];
				}
			}
			if($give>0){
				cfc('core')->member_credit($_SESSION['uid'],'credit2',$give,1,'jy_ailiao','会员充值赠送');
			}
		}
		//判断优惠券
		if($recharge['couponid']){
			$couponinfo=pdo_get('jy_weishop_coupon_getlog',array('id'=>$recharge['couponid'],'uniacid'=>$_SESSION['uniacid']));
			$couponinfo['item']=pdo_get('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$couponinfo['couponid']),array('backmoney','backcredit','backredpack'));
			if($couponinfo['item']['backmoney']>0){
				cfc('core')->member_credit($_SESSION['uid'],'credit2',$couponinfo['item']['backmoney'],1,'jy_ailiao','充值券赠送');
			}
			if($couponinfo['item']['backcredit']>0){
				cfc('core')->member_credit($_SESSION['uid'],'credit1',$couponinfo['item']['backcredit'],1,'jy_ailiao','充值券赠送');
			}
			if($couponinfo['item']['backredpack']>0){
				$params=array(
					'money'=>$couponinfo['item']['backredpack'],
					'mid'=>$_SESSION['uid'],
					'remark'=>'充值券赠送',
				);
				cfc('red_pack')->get_money($params);
			}
			pdo_update('jy_weishop_coupon_getlog',array('isuse'=>1),array('id'=>$recharge['couponid'],'uniacid'=>$_SESSION['uniacid']));
		}
		$event=array('mid'=>$_SESSION['uid'],'time'=>TIMESTAMP,'fee'=>$recharge['fee']);
		publish('core','member.recharge',$event);
		if($payresult['pay_type']=='ttpay'){
			//抖音小程序同步订单
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$recharge['wxapp_id'],'uniacid'=>$_SESSION['uniacid']),array('id','tt_appid','tt_paymchid','tt_paysecret'));
			$setting=array(
					'id'=>$wxapp['id'],
					'app_id'=>$wxapp['tt_appid'],
					'tt_paymchid'=>$wxapp['tt_paymchid'],
					'tt_paysecret'=>$wxapp['tt_paysecret'],
			);
			if($wxapp){
				$recharge['list'][]=array(
					'goodsid'=>108,
					'image'=>tomedia('../core/resource/images/welcomehdp02.jpg'),
					'title'=>'会员充值',
					'price'=>$recharge['fee'],
				);
				$recharge['status']=1;
				$recharge['price']=$recharge['fee'];
				$recharge['detail_url']='core/member/credit_log/index';
				$recharge['tid']=pdo_getcolumn('core_paylog',array('id'=>$recharge['pay_id'],'uniacid'=>$_SESSION['uniacid']),'tid');
				cfc('tt_pay',$setting)->push_order($recharge);
			}
		}
	}
	exi('充值成功！');
}